<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Storepro extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'storepro';
    
    // 自动写入时间戳字段
//    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden'), 'locked' => __('Locked'), 'deleted' => __('Deleted'), 'checking' => __('Checking')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    // 统计库存
    public static function getStock($storea, $factory)
    {

        $list = self::field("group_id, batch_id, field2, sum(cal_num) as stock ")
            ->where("factory_id", $factory)
            ->where("storea_id", $storea)
            ->group("batch_id")
            ->select();

        return $list;
    }
    // 统计库存-- 按品系
    public static function getStockByPinxi($storea, $factory)
    {

        $list = self::field("factory_id, storea_id , group_id, batch_id, storea_id, field2, sum(cal_num) as stock ")
            ->where("factory_id", $factory)
            ->where("storea_id", $storea)
            ->group("field2")
            ->select();

        return $list;
    }

}
